package org.seedltd.upms.controller.miniapp;

import lombok.AllArgsConstructor;
import org.seedltd.core.utils.BeanUtils;
import org.seedltd.core.validation.Save;
import org.seedltd.log.annotation.SysLog;
import org.seedltd.upms.model.detection.entity.DetectionOrder;
import org.seedltd.upms.model.detection.form.DetectionOrderForm;
import org.seedltd.upms.model.detection.form.MiniOrderSearchForm;
import org.seedltd.upms.model.detection.vo.DetectionOrderVO;
import org.seedltd.upms.model.detection.vo.DetectionTubeVO;
import org.seedltd.upms.service.detection.DetectionOrderLogService;
import org.seedltd.upms.service.detection.DetectionOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/mini-app/v1")
@PreAuthorize("hasRole('MINIAPPUSER')")
public class MiniAppOrderController {

    private final DetectionOrderService detectionOrderService;
    private final DetectionOrderLogService detectionOrderLogService;

    /**
     * 获取样本订单列表
     * 
     * @param form
     *            搜索条件
     * @param type
     *            1表示样本订单，0表示报告列表，2，待投递
     * @return
     */
    @PostMapping("/order/{type}")
    public List<DetectionOrderVO> order(@RequestBody MiniOrderSearchForm form, @PathVariable Integer type) {
        return detectionOrderService.page(form, type);
    }

    /**
     * 获取待投递的订单
     *
     * @return
     */
    @GetMapping("/order/deliverList")
    public List<DetectionOrderVO> orderDeliver() {
        return detectionOrderService.listOrderDeliver();
    }

    /**
     * 获取订单
     *
     * @param orderId
     *            订单主键
     * @return
     */
    @GetMapping("/order/{orderId}")
    public DetectionOrderVO getOrderById(@PathVariable Long orderId) {
        DetectionOrder detectionOrder = detectionOrderService.getById(orderId);
        DetectionOrderVO detectionOrderVO = new DetectionOrderVO();
        BeanUtils.copyProperties(detectionOrder, detectionOrderVO);
        return detectionOrderVO;
    }

    /**
     * 下单
     *
     * @param form
     *            订单管理表单数据
     * @return 订单管理主键
     */
    @SysLog("小程序下单")
    @PostMapping(value = "/createOrder")
    public Long add(@Validated({Save.class}) DetectionOrderForm form, HttpServletRequest request) throws Exception {
        return detectionOrderService.add(form, request);
    }

    /**
     * 获取订单报告数据
     *
     * @param orderId
     *            订单主键
     * @return
     */
    @GetMapping("/order/report/{orderId}")
    public DetectionTubeVO getReportOrder(@PathVariable Long orderId) {
        return detectionOrderService.getReportOrder(orderId);
    }

    /**
     * 检测账户下是否有未绑定的订单
     * 
     * @return true 有，false没有
     */
    @GetMapping("/order/check-unbading")
    public boolean checkUnBanding() {
        return detectionOrderService.checkUnBanding();
    }


    /**
     * 小程序用户删除订单
     * 
     * @param orderId
     *            订单主键
     * @return 是否删除成功
     */
    @SysLog("小程序用户删除订单")
    @GetMapping("/order/delete/{orderId}")
    public boolean deleteOrder(@PathVariable Long orderId) {
        return detectionOrderService.deleteByOrderId(orderId);
    }

}
